//
//  DSZDiZhiBJViewController.m
//  7.13苏宁框架
//
//  Created by app05 on 15/7/26.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZDiZhiBJViewController.h"
#import "UserModel.h"
#import "UserDB.h"
#import "DSZMoreView.h"
#import "DSZDiZhiGLViewController.h"

#import "DSZMainViewController.h"
#import "HomeViewController.h"
#import "DSZShopViewController.h"
@interface DSZDiZhiBJViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,tableViewIndexPathRowDelegate>


@property(nonatomic,strong)NSArray *provinces;
@property(nonatomic,strong)NSArray *cities;
@property(nonatomic,strong)NSArray *areas;
@property (weak, nonatomic) IBOutlet UIView *locationPickerView;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
//@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//@property (weak, nonatomic) IBOutlet UIImageView *arrow;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrow;

@property (weak, nonatomic) IBOutlet UITableViewCell *cell;

@property(nonatomic,assign)BOOL flag;

@property(nonatomic,weak)UIView *moreView;
@property(nonatomic,assign)BOOL f;

//@property (weak, nonatomic) IBOutlet UITextField *name;
//@property (weak, nonatomic) IBOutlet UITextField *TEL;
//@property (weak, nonatomic) IBOutlet UITextField *detailLoca;
//@property (weak, nonatomic) IBOutlet UITextField *postNum;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *TEL;
@property (weak, nonatomic) IBOutlet UITextField *detailLoca;
@property (weak, nonatomic) IBOutlet UITextField *postNum;
@property (weak, nonatomic) IBOutlet UITextField *tt;

- (IBAction)store;


@end
@implementation DSZDiZhiBJViewController


-(HZLocation *)locate
{
    if (_locate == nil) {
        _locate = [[HZLocation alloc] init];
    }
    
    return _locate;
}

-(void)viewDidLoad
{
    self.name.text=self.model.userName;
    self.TEL.text=self.model.telephone;
    self.postNum.text=self.model.postNum;
    self.titleLabel.text=self.model.bigLoca;
    self.detailLoca.text=self.model.littleLoca;
    self.name.enabled=NO;
    self.pickerView.delegate=self;
    self.pickerView.dataSource=self;
    //self.pickerView.frame=CGRectMake(0, 43, 320, 225);
    _provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
    _cities = [[_provinces objectAtIndex:0] objectForKey:@"cities"];
    
    self.locate.state = [[_provinces objectAtIndex:0] objectForKey:@"state"];
    self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
    
    _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
    if (_areas.count > 0) {
        self.locate.district = [_areas objectAtIndex:0];
    } else{
        self.locate.district = @"";
    }
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.cell addGestureRecognizer:tap];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 20;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [_provinces count];
            break;
        case 1:
            return [_cities count];
            break;
        case 2:
            return [_areas count];
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    switch (component) {
        case 0:
            return [[_provinces objectAtIndex:row] objectForKey:@"state"];
            break;
        case 1:
            return [[_cities objectAtIndex:row] objectForKey:@"city"];
            break;
        case 2:
            if ([_areas count] > 0) {
                return [_areas objectAtIndex:row];
                break;
            }
        default:
            return  @"";
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    switch (component) {
        case 0:
            _cities = [[_provinces objectAtIndex:row] objectForKey:@"cities"];
            [self.pickerView selectRow:0 inComponent:1 animated:YES];
            [self.pickerView reloadComponent:1];
            
            _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
            [self.pickerView selectRow:0 inComponent:2 animated:YES];
            [self.pickerView reloadComponent:2];
            
            self.locate.state = [[_provinces objectAtIndex:row] objectForKey:@"state"];
            self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
            if ([_areas count] > 0) {
                self.locate.district = [_areas objectAtIndex:0];
            } else{
                self.locate.district = @"";
            }
            break;
        case 1:
            _areas = [[_cities objectAtIndex:row] objectForKey:@"areas"];
            [self.pickerView selectRow:0 inComponent:2 animated:YES];
            [self.pickerView reloadComponent:2];
            
            self.locate.city = [[_cities objectAtIndex:row] objectForKey:@"city"];
            if ([_areas count] > 0) {
                self.locate.district = [_areas objectAtIndex:0];
            } else{
                self.locate.district = @"";
            }
            break;
        case 2:
            if ([_areas count] > 0) {
                self.locate.district = [_areas objectAtIndex:row];
            } else{
                self.locate.district = @"";
            }
            break;
        default:
            break;
    }
    self.titleLabel.text = [NSString stringWithFormat:@"所在地区：%@ %@ %@", self.locate.state, self.locate.city, self.locate.district];
    self.arrow.alpha=0;
    
    
}

-(void)tap:(UITapGestureRecognizer *)tap
{
    if (!self.flag) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        
        self.locationPickerView.frame=CGRectMake(0, 480-189, 320, 254);
        
        [UIView commitAnimations];
        self.flag=YES;
    }else
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        
        self.locationPickerView.frame=CGRectMake(0, 480, 320, 254);
        
        [UIView commitAnimations];
        self.flag=NO;
    }
    
}
-(void)setModel:(UserModel *)model
{
    _model=model;
    
    
    
    //self.name.enabled=NO;
}
- (IBAction)store {
    
    NSString *str=[NSString stringWithFormat:@"%@ %@ %@",self.locate.state,self.locate.city,self.locate.district];
    UserModel *userModel=[[UserModel alloc]init];
    userModel.userName=self.name.text;
    userModel.telephone=self.TEL.text;
    userModel.bigLoca=str;
    userModel.littleLoca=self.detailLoca.text;
    userModel.postNum=self.postNum.text;
    
    [[UserDB shareInstance] updataUser:userModel];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)back {
    //DSZMainHeaderView *main=[[DSZMainHeaderView alloc]init];
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)more {
    if (!self.f) {
        DSZMoreView *more=[DSZMoreView moreView];
        more.delegate=self;
        more.frame=CGRectMake(185, 65, 146, 186);
        self.moreView=more;
        [self.view addSubview:self.moreView];
        self.f=YES;
    }
    else
    {
        [self.moreView removeFromSuperview];
        self.moreView=nil;
        self.f=NO;
    }
    
}
-(void)dszMoreViewWithIndexRow:(NSInteger)indexPathRow
{
    if (indexPathRow==0) {
        UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DSZMainViewController *main=[story instantiateViewControllerWithIdentifier:@"main"];
        //DSZMainViewController *main=[[DSZMainViewController alloc]init];
        [self presentViewController: main animated:NO completion:nil];
    }else if (indexPathRow==1)
    {}
    else if (indexPathRow==2)
    {
        UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        DSZShopViewController *shop=[story instantiateViewControllerWithIdentifier:@"shop"];
        [self.navigationController pushViewController:shop animated:NO];
        self.flag=YES;
        [self more];
        self.navigationController.navigationBar.alpha=1;
        DSZMainViewController *main=(DSZMainViewController*)self.tabBarController;
        [main hiddenMainTabbar:YES];
    }else if (indexPathRow==3)
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
    
}
@end
